<template>
  <van-address-list
    v-model="chosenAddressId"
    :list="addresslist"
    @select="click"
    @add="onAdd"
    @edit="onEdit"
  ></van-address-list>
</template>
<script>
  import {Toast} from "vant"

  import appFunc from "../assets/app/func"
  import Address from "../assets/app/module/Address"

  export default {
    data() {
      return {
        chosenAddressId: 3,
        addressData: [],
        addresslist: []
      }
    },
    created: function () {
      appFunc.login();
      appFunc.isLogin();
      this.addressData = [
        Address(1, "山东省", "东营市", "东营区", "黄河路11111111", "257087", "张三", "15266008112"),
        Address(3, "山东省", "东营市", "东营区", "黄河路22222222", "257087", "李四", "1310000000"),
      ];
      this.addressData.forEach((address) => {
        this.addresslist.push(address.getData());
      });
      let currentAddress = appFunc.getAddress();
      this.chosenAddressId = currentAddress.id;
    },

    methods: {
      onAdd() {
        this.$router.push({path: "AddAddress"})
      },
      onEdit() {
        this.$router.push({path: "EditAddress"})
      },
      click() {
        this.addressData.forEach(address => {
          if (address.id === this.chosenAddressId) {
            appFunc.setAddress(address)
          }
        });
      }
    }
  }
</script>
